﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace TFSTools.TFSLibrary
{
    public sealed class TFSWorkItemTransition : TFSServerEntity
    {
        internal TFSWorkItemTransition(TFSWorkItemType parent, XmlNode transition)
            : base(parent.Connection)
        {
            this.Parent = parent;

            this.From = transition.Attributes["from"].Value;
            this.To = transition.Attributes["to"].Value;

            var reasonCollection = new List<TFSWorkItemTransitionReason>();

            foreach (XmlNode transitionChildNode in transition.ChildNodes)
            {
                if (transitionChildNode.Name == "REASONS")
                {
                    foreach (XmlNode reasonChildNode in transitionChildNode.ChildNodes)
                    {
                        if (reasonChildNode.Name == "DEFAULTREASON")
                        {
                            this.DefaultReason = new TFSWorkItemTransitionReason(reasonChildNode.Attributes["value"].Value, true);
                            reasonCollection.Add(this.DefaultReason);
                        }
                        else if (reasonChildNode.Name == "REASON")
                        {
                            reasonCollection.Add(new TFSWorkItemTransitionReason(reasonChildNode.Attributes["value"].Value, false));
                        }
                    }
                }
            }

            this.Reasons = new TFSWorkItemTransitionReasonCollection(reasonCollection);
        }

        public TFSWorkItemType Parent { get; private set; }

        public string To { get; private set; }

        public string From { get; private set; }

        public TFSWorkItemTransitionReasonCollection Reasons { get; private set; }

        public TFSWorkItemTransitionReason DefaultReason { get; private set; }

        public override string ToString() { return string.Format("From {0}, To {1}", this.From, this.To); }
    }

    public sealed class TFSWorkItemTransitionCollection : ReadOnlyCollection<TFSWorkItemTransition>
    {
        public TFSWorkItemType Parent { get; private set; }

        internal TFSWorkItemTransitionCollection(IList<TFSWorkItemTransition> list, TFSWorkItemType parent) : base(list) 
        {
            this.Parent = parent;
        }

        internal static TFSWorkItemTransitionCollection Create(TFSWorkItemType parent)
        {
            WorkItemType workItemType = parent.ServerEntity;
            XmlDocument workItemTypeXml = workItemType.Export(false);

            var transistionsCollection = new List<TFSWorkItemTransition>();
            XmlNodeList transitionsList = workItemTypeXml.GetElementsByTagName("TRANSITIONS");
            XmlNode transitions = transitionsList[0];

            foreach (XmlNode transition in transitions)
            {
                transistionsCollection.Add(new TFSWorkItemTransition(parent, transition));
            }

            return new TFSWorkItemTransitionCollection(transistionsCollection, parent);
        }
    }

    public sealed class TFSWorkItemTransitionReason
    {
        internal TFSWorkItemTransitionReason(string value, bool isDefault)
        {
            this.Value = value;
            this.IsDefault = isDefault;
        }

        public string Value { get; private set; }

        public bool IsDefault { get; private set; }
    }

    public sealed class TFSWorkItemTransitionReasonCollection : ReadOnlyCollection<TFSWorkItemTransitionReason>
    {
        internal TFSWorkItemTransitionReasonCollection(IList<TFSWorkItemTransitionReason> list) : base(list) { }
    }
}
